package com.procurement.inquiry.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.procurement.inquiry.domain.Chuanzhi;
import com.procurement.inquiry.domain.PcmInquiryDemand;
import com.procurement.inquiry.domain.PcmSupplier;
import com.procurement.inquiry.service.IPcmInquiryDemandService;
import com.ruoyi.system.api.domain.PcmAudit;
import com.ruoyi.system.api.domain.PcmSupplierQuotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.procurement.inquiry.domain.PcmInquiryForm;
import com.procurement.inquiry.service.IPcmInquiryFormService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 询价单Controller
 * 
 * @author ruoyi
 * @date 2023-11-28
 */
@RestController
@RequestMapping("/form")
public class PcmInquiryFormController extends BaseController
{
    @Autowired
    private IPcmInquiryFormService pcmInquiryFormService;
@Autowired
private IPcmInquiryDemandService pcmInquiryDemandService;
    /**
     * 查询询价单列表
     */
    @RequiresPermissions("inquiry:form:list2")
    @GetMapping(value = "/{query}")
    public TableDataInfo list(@PathVariable("query")String query)
    {

       // System.out.println(pcmInquiryDemand.getInquiryDemandNumber());
        System.out.println(query);
        List<PcmInquiryDemand> list = pcmInquiryFormService.selectPcmInquiryFormList(query);
        return getDataTable(list);
    }

    /**
     * 导出询价单列表
     */
//    @RequiresPermissions("system:form:export")
//    @Log(title = "询价单", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, PcmInquiryForm pcmInquiryForm)
//    {
//        List<PcmInquiryForm> list = pcmInquiryFormService.selectPcmInquiryFormList(pcmInquiryForm);
//        ExcelUtil<PcmInquiryForm> util = new ExcelUtil<PcmInquiryForm>(PcmInquiryForm.class);
//        util.exportExcel(response, list, "询价单数据");
//    }

    /**
     * 获取询价单详细信息
     */
    @RequiresPermissions("inquiry:form:list")
    @PostMapping
    public TableDataInfo getInfo(@RequestBody PcmSupplier Supplier)
    {
//        JSONObject jsonObject= (JSONObject) JSONObject.toJSON(Supplier);
       String suppliername=Supplier.getSupplierFullName();
        System.out.println("*****"+Supplier);
//        if(suppliername==null){
//            suppliername="";
//        }

        return getDataTable(pcmInquiryFormService.selectPcmInquiryFormByInquiryId(suppliername));
    }

    /**
     * 新增询价单
     */
//    @RequiresPermissions("system:form:add")
//    @Log(title = "询价单", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody PcmInquiryForm pcmInquiryForm)
//    {
//        return toAjax(pcmInquiryFormService.insertPcmInquiryForm(pcmInquiryForm));
//    }
    @Log(title = "询价单", businessType = BusinessType.INSERT)
    @PostMapping("/addAudit")
    public AjaxResult adds(@RequestBody Chuanzhi chuanzhi){
        System.out.println(chuanzhi.getInquiryDemandNumber());
        PcmInquiryDemand pcmInquiryDemand= pcmInquiryDemandService.selectPcmInquiryDemandByInquiryDemandNumber(chuanzhi.getInquiryDemandNumber());

        System.out.println(chuanzhi.getSuppliers());
        int a=pcmInquiryFormService.updatePcmApploicationDemand(pcmInquiryDemand.getPcmApplicationDetailList());
        chuanzhi.getSuppliers().forEach(e->{
            PcmSupplierQuotation pcmSupplierQuotation=new PcmSupplierQuotation();
            pcmSupplierQuotation.setInquiryDemandNumber(chuanzhi.getInquiryDemandNumber());
            //pcmSupplierQuotation.setQuotationDocument();
            pcmSupplierQuotation.setQuotingSupplier(e.getSupplierID2());
//          pcmSupplierQuotation.setEstimatedDeliveryTime();

            pcmInquiryFormService.insertPcmSupplierQuotation(pcmSupplierQuotation);
            PcmSupplierQuotation pcmSupplierQuotation1=pcmInquiryFormService.selectSupplierQuotation();
            System.out.println(11111);
            PcmAudit pcmAudit=new PcmAudit();
            pcmAudit.setUserId(e.getSupplierID2());
            pcmAudit.setAuditQuoteDeadline(new Date());
            pcmAudit.setAuditTableName("供应商报价");
            pcmAudit.setAuditTableYwName("pcm_supplier_quotation");
            pcmAudit.setAuditTableNumber(String.valueOf(pcmSupplierQuotation1.getQuotationID()));
            pcmAudit.setCirculationStatus(1L);
            pcmInquiryFormService.insertPcmAudit(pcmAudit);

        });

        return toAjax(1);
    }

    /**
     * 修改询价单
     */
//    @RequiresPermissions("system:form:edit")
//    @Log(title = "询价单", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody PcmInquiryForm pcmInquiryForm)
//    {
//        return toAjax(pcmInquiryFormService.updatePcmInquiryForm(pcmInquiryForm));
//    }

    /**
     * 删除询价单
     */
    @RequiresPermissions("system:form:remove")
    @Log(title = "询价单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{inquiryIds}")
    public AjaxResult remove(@PathVariable Long[] inquiryIds)
    {
        return toAjax(pcmInquiryFormService.deletePcmInquiryFormByInquiryIds(inquiryIds));
    }
}
